/****************************************************************************/
/*This program merges the tornado and business data together and            */
/*prepares it for the regression program                                    */
/****************************************************************************/
 

#delimit;
clear;
set matsize 800;
set more off;
capture log close;
set trace off;

/****************************************************************************/
/* DEFINE THE PROJECT FOLDER                                                */
/****************************************************************************/

local dropboxpath C:\Users\srohlin\Dropbox\Tornadoes\PreRR;
local pathsmr  C:\Research;

/****************************************************************************/
/*DEFINE PATH TO FOLDERS                                                    */
/****************************************************************************/

local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM;

/****************************************************************************/
/*Open tornado data to cut the business data before the fillin command      */
/****************************************************************************/

use `Torpath'\Data\tornado_data_cleaned_0515MilesBuffer.dta, clear;

capture drop _merge;

sort bgfips tornado_id;
by bgfips: gen temp = _n;

keep if temp == 1;

save `Torpath'\Temp\tornado_data_temp_allinfo_1.dta, replace;

keep bgfips temp;

sort bgfips;
save `Torpath'\Temp\tornado_data_temp_1.dta, replace;

/****************************************************************************/
/*Open business data                                                        */
/*Even though it says wide panel it means in relation to industry not year  */
/****************************************************************************/

use `Torpath'\Data\bgfips_level_allyears_wide_final.dta, clear;

/****************************************************************************/
/*Merge on tornado data first to reduce file size                           */
/****************************************************************************/

replace bgfips = "0" + bgfips if length(bgfips) == 14; /*where error occured*/
sort bgfips year;

merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_1.dta;
tab _merge;    /*We have some 2s which means that some blocks don't have businesses which is fine*/
keep if _merge == 3;
drop _merge temp;

/****************************************************************************/
/*Use fillin command to create a balanced panel                             */
/****************************************************************************/

drop if year ==.;

sort year bgfips;
capture fillin year bgfips;

/****************************************************************************/
/*Merge on tornado data to add information to the filled in data            */
/****************************************************************************/

sort bgfips year;
merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_allinfo_1.dta;
tab _merge;

keep if _merge == 3;
drop _merge;

sort bgfips;
save `Torpath'\Temp\business_tornado_data_1.dta, replace;


/*Some blocks experienced more than one tornadoes*/

use `Torpath'\Data\tornado_data_cleaned_0515MilesBuffer.dta, clear;

capture drop _merge;

sort bgfips tornado_id;
by bgfips: gen temp = _n;

keep if temp == 2;

save `Torpath'\Temp\tornado_data_temp_allinfo_2.dta, replace;

keep bgfips temp;

sort bgfips;
save `Torpath'\Temp\tornado_data_temp_2.dta, replace;


/****************************************************************************/
/*Open business data                                                        */
/*Even though it says wide panel it means in relation to industry not year  */
/****************************************************************************/

use `Torpath'\Data\bgfips_level_allyears_wide_final.dta, clear;

/****************************************************************************/
/*Merge on tornado data first to reduce file size                           */
/****************************************************************************/

replace bgfips = "0" + bgfips if length(bgfips) == 14;/*where error occured*/
sort bgfips year;

merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_2.dta;  
tab _merge;    /*We have some 2s which means that some blocks don't have businesses which is fine*/
keep if _merge == 3;
drop _merge temp;


/****************************************************************************/
/*Use fillin command to create a balanced panel                             */
/****************************************************************************/

drop if year ==.;

sort year bgfips;
capture fillin year bgfips;

/****************************************************************************/
/*Merge on tornado data to add information to the filled in data            */
/****************************************************************************/

sort bgfips year;
merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_allinfo_2.dta;
tab _merge;

keep if _merge == 3;
drop _merge;

append using `Torpath'\Temp\business_tornado_data_1.dta;

/****************************************************************************/
/*To keep file small we drop the years we currently are not using           */
/*This is currently based on 12 tornados using 3 years before and after     */
/****************************************************************************/

tab tor_year;

/****************************************************************************/
/*Replace missings with zeros                                               */
/****************************************************************************/

global outcomestubs "firm emp sales firm_1y emp_1y sales_1y firm_2to3y emp_2to3y sales_2to3y firm_4plusy emp_4plusy sales_4plusy" ;
global industrystubs "all manuf retail service nonmanuf nongov noncngv public mining const trans whole agric fire" ;


foreach i of global outcomestubs { ;
foreach j of global industrystubs { ;
replace `i'_`j' = 0 if `i'_`j' ==. ;
};
};

/****************************************************************************/
/*save cleaned tornado data                                                 */
/****************************************************************************/

sort tornado_id year bgfips;
save `Torpath'\Data\business_tornado_data_final.dta, replace;

log close;



